package cn.com.dhcc.app.pub.core.service;

import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.handler.cachehandler.CacheKey;
import cn.com.dhcc.app.core.handler.cachehandler.MemoryCacheService;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.registry.vo.PipeLineFtp;

/**
 * 内存缓存
 * @author qiao
 * @2014-6-18 @下午2:35:05
 */
@Service(AppIocBean.TURBO_CACHE_SERVICE)
public class TurboCacheService extends MemoryCacheService{
	/**
	 * 更新缓存对象为节点的缓存
	 * @param cacheOperType
	 * @param nodeInfo
	 */
	public void updateNodeCache(CacheOperType cacheOperType,NodeInfo nodeInfo){
		if(CacheOperType.UPDATE==cacheOperType){
			putData("更新节点信息缓存code:"+nodeInfo.getNodeCode(), KEY_VALUE_TYPE.NODE_CODE__NODEINFO, nodeInfo.getNodeCode(), nodeInfo);
			putData("更新节点信息缓存id:"+nodeInfo.getId(), KEY_VALUE_TYPE.NODE_ID__NODEINFO, nodeInfo.getNodeCode(), nodeInfo);
		}else{
			throw new RuntimeException("节点不支持删除");
		}
	}
	/*public void updateSysInfoCache(CacheOperType cacheOperType,SysInfo sysInfo){
		if(CacheOperType.UPDATE==cacheOperType){
			putData("更新系统信息缓存code:"+sysInfo.getSysCode(), KEY_VALUE_TYPE.SYS_CODE__SYSINFO, sysInfo.getSysCode(), sysInfo);
			putData("更新系统信息缓存id:"+sysInfo.getId(), KEY_VALUE_TYPE.SYS_ID__SYSINFO, sysInfo.getId(), sysInfo);
		}else{
			removeData("删除系统信息缓存code:"+sysInfo.getSysCode(), KEY_VALUE_TYPE.SYS_CODE__SYSINFO, sysInfo.getSysCode());
			removeData("删除系统信息缓存id:"+sysInfo.getId(), KEY_VALUE_TYPE.SYS_ID__SYSINFO, sysInfo.getId());
		}
	}*/
	/**
	 * 
	 * @param cacheOperType
	 * @param serviceInfo
	 * @param sysId 系统id
	 *//*
	public void updateSysServiceInfoCache(CacheOperType cacheOperType,SysServiceInfo serviceInfo,SysInfo sysInfo){
		if(CacheOperType.UPDATE==cacheOperType){
			putData("更新系统服务信息缓存id:"+sysInfo.getId(), KEY_VALUE_TYPE.SYS_ID__SYSSERVICE, sysInfo.getId(), serviceInfo);
			putData("更新系统服务缓存code:"+sysInfo.getSysCode(), KEY_VALUE_TYPE.SYS_CODE__SYSSERVICE, sysInfo.getSysCode(), serviceInfo);
		}else{
			removeData("删除系统服务信息缓存id:"+sysInfo.getId(), KEY_VALUE_TYPE.SYS_ID__SYSSERVICE, sysInfo.getId());
			removeData("删除系统服务缓存code:"+sysInfo.getSysCode(), KEY_VALUE_TYPE.SYS_CODE__SYSSERVICE, sysInfo.getSysCode());
		}
	}*/
	public void updateFtpCache(CacheOperType cacheOperType,PipeLineFtp pipeLineFtp,String nodeCode){
		if(CacheOperType.UPDATE==cacheOperType){
			putData("更新ftp基本信息缓存", KEY_VALUE_TYPE.NODE_CODE__PIPELINE_FTP, nodeCode, pipeLineFtp);
		}else{
			removeData("删除ftp基本信息缓存", KEY_VALUE_TYPE.NODE_CODE__PIPELINE_FTP,nodeCode);
		}
	}
	public void putAuthData(String desc, String key, Object value) {
		putData(desc, KEY_VALUE_TYPE.AUTH, key, value);
	}
	public Object getAuthData(String desc, String key) {
		Object rs = getData(desc, KEY_VALUE_TYPE.AUTH, key);
		return rs;
	}
	public void removeAuthData(String desc, String key) {
		removeData(desc, KEY_VALUE_TYPE.AUTH, key);
	}
	//---------------------------------------------------
	public void putData(String desc,KEY_VALUE_TYPE cacheType, String key, Object value) {
		if(!cacheType.valueType.isInstance(value)){
			throw new RuntimeException("插入缓存的值和支撑的类型不一致,需要类型:"+cacheType.valueType+"，实际类型："+value.getClass());
		}
		super.putData(desc, cacheType.keyType, key, value);
	}
	public Object getData(String desc,KEY_VALUE_TYPE cacheType,String key) {
		return super.getData(desc, cacheType.keyType, key);
	}
	
	public void removeData(String desc,KEY_VALUE_TYPE cacheType, String key) {
		super.removeData(desc, cacheType.keyType, key);
	}
	public static enum KEY_VALUE_TYPE{
		NODE_CODE__NODEINFO("NODE_CODE__NODEINFO","节点编码-节点对象",NodeInfo.class),
		NULL__LOCAL_NODEINFO("NULL__LOCAL_NODEINFO","当前节点对象",NodeInfo.class),
		NODE_ID__NODEINFO("NODE_ID__NODEINFO","节点id-节点对象",NodeInfo.class),
		/*SYS_CODE__SYSINFO("SYS_CODE__SYSINFO","系统编码-系统对象",SysInfo.class),
		SYS_ID__SYSINFO("SYS_ID__SYSINFO","系统id-系统对象",SysInfo.class),*/
		/*SYS_CODE__SYSSERVICE("SYS_CODE__SYSSERVICE","系统编码-系统服务对象",SysServiceInfo.class),*/
		/*SYS_ID__SYSSERVICE("SYS_ID__SYSSERVICE","系统id-系统服务对象",SysServiceInfo.class),*/
		NODE_CODE__PIPELINE_FTP("NODE_CODE__PIPELINE_FTP","节点编码-FTP通道对象",PipeLineFtp.class),
		AUTH("_AUTH_","权限资源管理");
		String keyType;//缓存分类，必须唯一
		String desc;
		Class<?> valueType;//值类型
		private KEY_VALUE_TYPE(String keyType,String desc,Class<?> valueType){
			this.keyType = keyType;
			this.desc = desc;
			this.valueType = valueType;
		}
		private KEY_VALUE_TYPE(String keyType,String desc){
			this.keyType = keyType;
			this.desc = desc;
		}
	}
	//注册缓存类型
	static{
		KEY_VALUE_TYPE[] cacheTypes = KEY_VALUE_TYPE.values();
		for (int i = 0; i < cacheTypes.length; i++) {
			KEY_VALUE_TYPE cacheType = cacheTypes[i];
			registCacheKey(new CacheKey(cacheType.keyType, cacheType.desc));
		}
	}
	
}
